package com.ptswitch.material.services;

import javax.servlet.ServletContext;

import org.apache.tapestry5.NullFieldStrategy;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.annotations.Path;
import org.apache.tapestry5.internal.services.IgnoredPathsFilter;
import org.apache.tapestry5.ioc.Configuration;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.Contribute;
import org.apache.tapestry5.ioc.annotations.ImportModule;
import org.apache.tapestry5.ioc.annotations.Local;
import org.apache.tapestry5.ioc.annotations.Marker;
import org.apache.tapestry5.ioc.annotations.Startup;
import org.apache.tapestry5.ioc.services.ApplicationDefaults;
import org.apache.tapestry5.ioc.services.ServiceOverride;
import org.apache.tapestry5.services.HttpServletRequestFilter;
import org.apache.tapestry5.services.NullFieldStrategySource;
import org.apache.tapestry5.services.RequestExceptionHandler;
import org.apache.tapestry5.services.javascript.JavaScriptModuleConfiguration;
import org.apache.tapestry5.services.javascript.ModuleManager;
import org.hxzon.tapestry5.services.HxzonTapestryModule;
import org.hxzon.tapestry5.util.EmptyNullFieldStrategy;
import org.hxzon.tapestry5.util.MyRequestExceptionHandler;
import org.slf4j.Logger;

import com.ptswitch.material.service.PermissionService;
import com.ptswitch.material.service.RoleService;
import com.ptswitch.material.service.UserService;
import com.ptswitch.material.util.MaterialUtil;
import com.ptswitch.material.util.WebUtil;

@ImportModule({ HxzonTapestryModule.class })
public class AppModule {
    @Startup
    public static void init(ServletContext servletContext, Logger logger) {
        String contextRealPath = servletContext.getRealPath("");
        logger.info("contextPath:" + contextRealPath);
        if (contextRealPath == null) {
            contextRealPath = "";
        }
        WebUtil.init(contextRealPath, contextRealPath + "/WEB-INF/upload/");
        MaterialUtil.init(contextRealPath);
    }

    @Startup
    public static void init(PermissionService permissionSer, RoleService roleSer, UserService userSer) {
        permissionSer.initPermissions();
        roleSer.initRoles();
        userSer.initUsers();
    }

    public static void bind(ServiceBinder binder) {
        binder.bind(RequestExceptionHandler.class, MyRequestExceptionHandler.class).withId("MyRequestExceptionHandler");
        //binder.bind(URLEncoder.class, Base64UrlEncoderImpl.class).withId("MyURLEncoder");
    }

    @ApplicationDefaults
    public static void contributeApplicationDefaults(MappedConfiguration<String, String> configuration) {
        configuration.add(SymbolConstants.COOKIE_MAX_AGE, "14 d");
        configuration.add(SymbolConstants.HMAC_PASSPHRASE, "mstar");
        configuration.add(SymbolConstants.JAVASCRIPT_INFRASTRUCTURE_PROVIDER, "jquery");
    }

    @Contribute(NullFieldStrategySource.class)
    public static void contributeNullFieldStrategySource(MappedConfiguration<String, NullFieldStrategy> configuration) {
        configuration.override("default", new EmptyNullFieldStrategy());
    }

    @Contribute(HttpServletRequestFilter.class)
    @Marker(IgnoredPathsFilter.class)
    public static void contributeIgnoredPathsFilter(Configuration<String> configuration) {
        configuration.add("/zkau/web/.*");
        configuration.add("/zkau/.*");
        configuration.add("/zkau");
//        configuration.add(".*\\.css");
//        configuration.add(".*\\.js");
    }

    @Contribute(ServiceOverride.class)
    public static void contributeServiceOverride(MappedConfiguration<Class<?>, Object> configuration, //
            @Local RequestExceptionHandler overrideRequestExceptionHandler) {//
        //@Local URLEncoder urlEncoder) {
        configuration.add(RequestExceptionHandler.class, overrideRequestExceptionHandler);
        //configuration.add(URLEncoder.class, urlEncoder);
    }

    //t5.4
    @Contribute(ModuleManager.class)
    public static void setupBaseModules(MappedConfiguration<String, Object> configuration, @Path("context:js/mstar.js") Resource mstar) {
        configuration.add("mstar", new JavaScriptModuleConfiguration(mstar).dependsOn("jquery"));
    }
}
